package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.basic.wcs.utils.LoggerUtil;
import com.suray.wcs.service.enums.MachineTaskStatus;
import com.suray.wcs.service.enums.ProductType;
import com.suray.wcs.service.enums.TaskStatusGroup;
import com.suray.wcs.service.system.bo.QueryMachineTasksBO;
import com.suray.wcs.service.system.entity.MachineTaskDB;
import com.suray.wcs.service.system.mapper.MachineTaskDBMapper;
import com.suray.wcs.service.system.service.MachineTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class MachineTaskServiceImpl extends ServiceImpl<MachineTaskDBMapper, MachineTaskDB> implements MachineTaskService {
    private static Logger logger = LoggerFactory.getLogger(MachineTaskServiceImpl.class);

    @Autowired
    private MachineTaskDBMapper machineTaskDBMapper;

    @Override
    public IPage<MachineTaskDB> queryMachineTasks(QueryMachineTasksBO machineTasksBO) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<MachineTaskDB> queryWrapper = new QueryWrapper<>();
        if (machineTasksBO.getTaskId() != null) {
            queryWrapper.lambda().like(MachineTaskDB::getTaskId, machineTasksBO.getTaskId());
        }
        if (!StringUtils.isEmpty(machineTasksBO.getStartStation())) {
            queryWrapper.lambda().like(MachineTaskDB::getStartStation, machineTasksBO.getStartStation());
        }
        if (!StringUtils.isEmpty(machineTasksBO.getEndStation())) {
            queryWrapper.lambda().like(MachineTaskDB::getEndStation, machineTasksBO.getEndStation());
        }
        if (machineTasksBO.getProductType() != null) {
            queryWrapper.lambda().eq(MachineTaskDB::getProductType, machineTasksBO.getProductType());
        }
        if (machineTasksBO.getTaskStatus() != null) {
            queryWrapper.lambda().eq(MachineTaskDB::getTaskStatus, machineTasksBO.getTaskStatus());
        }
        if (machineTasksBO.getInsertTimeStart() != null) {
            queryWrapper.lambda().ge(MachineTaskDB::getCreateTime, formatter.format(machineTasksBO.getInsertTimeStart()));
        }
        if(!StringUtils.isEmpty(machineTasksBO.getMachineNo())){
            queryWrapper.lambda().like(MachineTaskDB::getMachineNo, machineTasksBO.getMachineNo());
        }
        if (machineTasksBO.getInsertTimeEnd() != null) {
            queryWrapper.lambda().lt(MachineTaskDB::getCreateTime, formatter.format(machineTasksBO.getInsertTimeEnd()));
        }
        if (machineTasksBO.getTaskStatusGroup() != null) {
            if (machineTasksBO.getTaskStatusGroup() == TaskStatusGroup.UN_FINISH.getProgressNum()) {
                queryWrapper.and(Wrapper -> Wrapper.and(ww -> ww.lambda().ne(MachineTaskDB::getTaskStatus, MachineTaskStatus.TASK_FINISH.getValue())));
            } else {
                queryWrapper.lambda().and(ww -> ww.eq(MachineTaskDB::getTaskStatus, MachineTaskStatus.TASK_FINISH.getValue()));
            }
        }
        Page<MachineTaskDB> page = new Page<>(machineTasksBO.getPageBO().getCurrentPage(), machineTasksBO.getPageBO().getPageSize());
        List<MachineTaskDB> machineTaskDBS = machineTaskDBMapper.selectPage(page, queryWrapper).getRecords();
        IPage<MachineTaskDB> machineTaskDBIPage = machineTaskDBMapper.selectPage(page, queryWrapper);

        for(MachineTaskDB machineTaskDB : machineTaskDBS){
            if(machineTaskDB.getProductType() != null){
                machineTaskDB.setProductTypeDes(ProductType.parseProductType(machineTaskDB.getProductType()).toString());
            }
            if(machineTaskDB.getTaskStatus() != null){
                machineTaskDB.setTaskStatusDes(MachineTaskStatus.parseMachineTaskStatus(machineTaskDB.getTaskStatus()).toString());
            }
        }
        machineTaskDBIPage.setRecords(machineTaskDBS);
        return machineTaskDBIPage;
    }

    @Override
    public int deleteMachineTasks(List<Integer> taskId) {
        return machineTaskDBMapper.deleteBatchIds(taskId);
    }

    @Override
    public MachineTaskDB queryMachineTaskInfoById(Integer id) {
        return machineTaskDBMapper.selectById(id);
    }

    @Override
    public int updateMachineTaskInfo(MachineTaskDB machineTaskDB) {
        return machineTaskDBMapper.updateById(machineTaskDB);
    }

    @Override
    public synchronized void insertMachineTask(List<MachineTaskDB> machineTaskDBS) {
        machineTaskDBS.forEach(task -> {
            if (task != null) {

                if (task.getCreateTime() == null) {
                    task.setCreateTime(new Date());
                }
                if(task.getTaskStatus() == null){
                    task.setTaskStatus(MachineTaskStatus.WAIT_SEND.getValue());
                }

                this.save(task);

                this.updateById(task);
                LoggerUtil.info("-->插入机械臂任务" + task, this.getClass());
            }
        });
    }

    @Override
    public synchronized void updateMachineTaskStatus(MachineTaskDB machineTaskDB, Integer taskStatus) {
        machineTaskDB.setTaskStatus(taskStatus);
        MachineTaskDB currentMachineTask = new MachineTaskDB();
        currentMachineTask.setTaskId(machineTaskDB.getTaskId());
        currentMachineTask.setTaskStatus(taskStatus);
        boolean result = this.updateById(currentMachineTask);
        logger.info("更新机械臂任务：" + machineTaskDB + "状态为:" + taskStatus + ",更新结果：" + result);
    }
}
